<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/'+goods.categories[1].id">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/'+goods.categories[0].id">{{goods.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales :goods="goods"/>
        </div>
        <div class="spec">
          <GoodsName :goods="goods"/>
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="changeSku" />
          <!-- 数量选择组件 -->
          <XtxNumbox label="数量" v-model="num" :max="goods.inventory"/>
          <!-- 加入购物车按钮组件 -->
          <XtxButton @click="insertCart()" type="primary" style="margin-top:20px;">加入购物车</XtxButton>
          <!-- 加入收藏按钮组件 -->
          <XtxButton @click="insertCollect()" type="plain" style="margin-top:20px; margin-left: 15px">加入收藏</XtxButton>
        </div>
      </div>
      <!-- 同类商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs :goods="goods"/>
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24小时热销榜+周热销榜 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1"/>
          <GoodsHot :goodsId="goods.id" :type="2"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { nextTick, provide, ref, watch } from 'vue'
import GoodsRelevant from './components/goods-relevant.vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsWarn from './components/goods-warn.vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { findGoodsDetail } from '@/api/product'
import Message from '@/components/library/message'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsHot,
    GoodsTabs,
    GoodsWarn
  },
  setup () {
    const goods = getGoodsDetail()
    // sku改变时触发
    const changeSku = (sku) => {
      // 改变sku时对应切换产品的价格及库存信息
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      // 记录选择后的sku，可能有数据，可能没有数据{}
      currSku.value = sku
    }
    // 提供goods数据给隔代组件使用
    provide('goods', goods)
    // 选择的数量
    const num = ref(1)
    // 加入购物车逻辑
    const currSku = ref(null)
    const store = useStore()
    const insertCart = () => {
      // 约定加入购物车字段必须与服务端所需字段一致
      if (currSku.value && currSku.value.skuId) {
        const { skuId, specsText: attrsText, inventory: stock } = currSku.value
        const { id, name, price, mainPictures } = goods.value
        store.dispatch('cart/insertCart', {
          skuId,
          attrsText,
          stock,
          id,
          name,
          price,
          nowPrice: price,
          picture: mainPictures[0],
          count: num.value,
          selected: true,
          isEffective: true
        }).then(() => {
          Message({ type: 'success', text: '加入购物车成功' })
        })
      } else {
        Message({ text: '请选择完整规格' })
      }
      if (num.value > goods.inventory) {
        Message({ text: '当前商品库存不足' })
      }
    }
    // 加入收藏逻辑
    const insertCollect = () => {
      // 没有对应请求接口，无逻辑
      Message({ type: 'success', text: '收藏成功' })
    }
    return { goods, changeSku, num, insertCart, insertCollect }
  }
}
// 把获取商品详情函数方法单独脱离出setup
const getGoodsDetail = () => {
  const goods = ref(null)
  const route = useRoute()
  watch(() => route.params.id, (newVal) => {
    if (newVal && `/product/${newVal}` === route.path) {
      findGoodsDetail(route.params.id).then(data => {
        // 让商品数据为null然后使用v-if的组件可以重新销毁和创建
        goods.value = null
        nextTick(() => {
          goods.value = data.result
        })
      })
    }
  }, { immediate: true })
  return goods
}
</script>

<style lang="less" scoped>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
</style>
